package cn.yc.writer;

import java.io.FileWriter;
import java.io.IOException;

public class FileWriter_ {
    public static void main(String[] args) {
        String targetFilePath = "e:\\io\\note.txt";
        String words = "风雨兼程，逆水行舟！";
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(targetFilePath);
            fileWriter.write(words);
            // 写入单个字符
            fileWriter.write('a');
            // 写入指定数组
            char[] chars = new char[]{'a', 'b', 'c', 'd', 'e'};
            fileWriter.write(chars);
            // 写指定数组的指定部分
            fileWriter.write("努力学习Java".toCharArray(), 4, 4);
            // 写入字符串的指定部分
            fileWriter.write(words, 0, 4);
            // 写入整个字符串
            fileWriter.write("加油向前冲".toString(),0,2);
            System.out.println("写入完成!");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileWriter != null) {
                try {
                    // 必须close或者flush才能保存到文件中
                    fileWriter.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
